/**
 * crawler
 *
 * outlook@China software studio
 * Copyright (c) 2008-2010. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.crawler.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.outlook.common.log.LogUtil;
import org.outlook.crawler.client.domain.Source;
import org.outlook.crawler.client.pojo.HttpStatus;
import org.outlook.crawler.client.pojo.Response;
import org.outlook.crawler.client.pojo.ResponseType;
import org.outlook.crawler.client.pojo.Status;
import org.outlook.crawler.client.pojo.Webinfo;
import org.outlook.crawler.database.CrawlerDataSource;
import org.outlook.crawler.task.CrawlTask;
import org.outlook.crawler.task.TaskFactory;
import org.outlook.crawler.task.TaskStatus;
import org.outlook.crawler.util.Fetcher;

/**
 * function:
 * @author Watson email: watson.wu@hotmail.com
 * @date 2011
 */
public class CrawlerServiceImpl implements CrawlerService {

	private Log logger = LogFactory.getLog(CrawlerServiceImpl.class);
	
	private TaskFactory taskFactory;
	private CrawlerDataSource crawlerDatasource;

	public void setTaskFactory(TaskFactory taskFactory) {
		this.taskFactory = taskFactory;
	}

	public void setCrawlerDatasource(CrawlerDataSource crawlerDatasource) {
		this.crawlerDatasource = crawlerDatasource;
	}

	@Override
	public Response crawl(CrawlTask task) {
		if(task == null) return Response.newErrorInstance();
		
		if(!task.getResponseType().isSynchr()) {
			taskFactory.add(task);
			return new Response(null, Status.PEND);
		}

		Source source = task.getSource();
		
		//update task status
		taskFactory.updateTaskStatus(task, TaskStatus.DOING);
		
		//get real URL for fetching
		String requestURL = getRequestURL(task);
		
		Fetcher fetcher = new Fetcher();
		Webinfo webinfo = fetcher.fetch(requestURL, source);
		if(null == webinfo) {
			//TODO
			LogUtil.debug(logger, "not fetch any webinfo");
		} else if(webinfo.getStatus() == HttpStatus.SC_OK) {
			//TODO
			
			taskFactory.updateTaskStatus(task, TaskStatus.DONE);
		} else if(webinfo.getStatus() != HttpStatus.SC_OK) {
			//TODO
		}
		return null;
	}

	@Override
	public Response crawl(String url, Long sourceId, ResponseType responseType) {
		CrawlTask task = new CrawlTask();
		Source source = crawlerDatasource.getSourceById(sourceId);
		task.setSource(source);
		task.setResponseType(responseType);
		task.setUrl(url);

		return crawl(task);
	}

	@Override
	public Response crawl(String from, String to, Long sourceId,
			ResponseType responseType) {
		CrawlTask task = null;
		// TODO Auto-generated method stub
		return crawl(task);
	}

	@Override
	public Webinfo getWebinfo(String url, Long sourceId) {
		CrawlTask task = null;
		// TODO Auto-generated method stub
		crawl(task);
		return null;
	}

	@Override
	public boolean isExisted(String from, String to, Long sourceId) {
		// TODO Auto-generated method stub
		return false;
	}

	private String getRequestURL(CrawlTask task) {
		return task.getUrl();
	}

	private boolean hasResult(Status status) {
		return status.equals(Status.OK) || status.equals(Status.RESULT);
	}

}
